function startAll() {
	alert('点你妹!')
}

function stopAll() {
	alert('点你妹!')
}

function addAccount() {
	$('#add-account-modal').modal('toggle');
}

function refresh() {
	alert('点你妹!')
}

$(document).ready(function() {

	// App.ajax(base + 'api/getData', 'get', {'name' : userLoginId}, function(d) {
	// 	console.log(d)
	// });


	//try to get all
	App.ajax(base + 'api/getAll', 'get', {}, function(arr) {

		for (var i = 0; i < arr.length; i ++) {
			var data = arr[i];

			var Tools = {}, ToolsCount = 0;
			for (var t = 0; t < data._TOOLS.length; t ++) {
				if (!data._TOOLS[t]) continue;
				Tools[data._TOOLS[t].name] = data._TOOLS[t].count;
				ToolsCount += data._TOOLS[t].count;
			}

			var wheatCount = 0;
			for (var t = 0; t < data._SILOS.length; t++) {
				if (data._SILOS[t] && data._SILOS[t].name == 'Wheat') wheatCount = data._SILOS[t].count;
			}

			var Materials = {}, materialsCount = 0;
			var expandField = {}, expandFieldCount = 0;
			var townMaterials = {}, townMCount = 0;

			var materialArr = ['Wood Panel', 'Duct Tape', 'Screw', 'Bolt', 'Nail', 'Plank'],
				expandArr = ['Marker Stake', 'Mallet', 'Land Deed'],
				townMArr = ['Tar Bucket', 'Brick', 'Stone Block', 'Paint Bucket', 'Hand Drill', 'Hammer', 'Map Piece'];

			var cargoToal = 0;

			for (var t = 0; t < data._MATERIALS.length; t ++) {
				var o = data._MATERIALS[t];

				if (materialArr.indexOf(o.name) != -1) {
					Materials[o.name] = o.count;
					materialsCount += o.count;
				}

				if (expandArr.indexOf(o.name) != -1) {
					expandField[o.name] = o.count;
					expandFieldCount += o.count;
				}

				if (townMArr.indexOf(o.name) != -1) {
					townMaterials[o.name] = o.count;
					townMCount += o.count;
				}

				cargoToal += o.count;
			}


			var template =
				'<div class="panel border">' +
				'<div class="panel-heading">' +
				'<h4 class="panel-title">' +
				'<a data-toggle="collapse" data-parent="#accordion2" href="#' + 'collapse' + i + '" class="collapsed">' +
				'账号: ' + data._FARMNAME + '' +
				'</a>' +
				'</h4>' +
				'</div>' +
				'<div id="' + 'collapse' + i + '" class="panel-collapse collapse" style="height: 0px;">' +
				'<div class="panel-body">' +
				'<div class="row" style="margin: -10px 0px 10px 0px">' +
				'<button type="button" class="btn btn-success btn-block width60" style="display: inline;margin-top:5px;">开始</button>' +
				'<button type="button" class="btn btn-danger btn-block width60" style="display: inline;">停止</button>' +
				'<button type="button" class="btn btn-primary btn-block width60" style="display: inline;float: right;">设置</button>' +
				'</div>' +
				'<div class="row bottom20">' +
				'<div class="col-md-12">' +
				'<div class="list-group">' +
				'<a href="#" class="list-group-item border">' +
				'<h4 class="list-group-item-heading">基本信息</h4>' +
				'<p class="list-group-item-text">' +
				'<table class="table" style="font-size: 13px;margin-bottom: -10px;">' +
				'<tbody>' +
				'<tr>' +
				'<td>名称: ' + data._FARMNAME + '</td>' +
				'<td>等级: ' + data._BASICINFO_.level + '级</td>' +
				'</tr>' +
				'<tr>' +
				'<td>金币: ' + data._BASICINFO_.coin + '</td>' +
				'<td>钻石: ' + data._BASICINFO_.crystal + '</td>' +
				'</tr>' +
				'</tbody>' +
				'</table>' +
				'</p>' +
				'</a>' +
				'<a href="#" class="list-group-item border">' +
				'<h4 class="list-group-item-heading">建材: ' + materialsCount + '</h4>' +
				'<p class="list-group-item-text">' +
				'<table class="table" style="font-size: 13px;margin-bottom: -10px;">' +
				'<tbody>' +
				'<tr>' +
				'<td>螺栓: ' + Materials.Screw + '</td>' +
				'<td>木板: ' + Materials.Plank + '</td>' +
				'<td>胶带: ' + Materials['Duct Tape'] + '</td>' +
				'</tr>' +
				'<tr>' +
				'<td>盒钉: ' + Materials.Bolt + '</td>' +
				'<td>镶板: ' + Materials['Wood Panel'] + '</td>' +
				'<td>螺钉: ' + Materials.Nail + '</td>' +
				'</tr>' +
				'</tbody>' +
				'</table>' +
				'</p>' +
				'</a>' +
				'<a href="#" class="list-group-item border">' +
				'<h4 class="list-group-item-heading">工具: ' + ToolsCount + '</h4>' +
				'<p class="list-group-item-text">' +
				'<table class="table" style="font-size: 13px;margin-bottom: -10px;">' +
				'<tbody>' +
				'<tr>' +
				'<td>炸药: ' + Tools.Dynamite + '</td>' +
				'<td>TNT: ' + Tools['Demolition Charge'] + '</td>' +
				'<td>铲子: ' + Tools.Shovel + '</td>' +
				'</tr>' +
				'<tr>' +
				'<td>斧子: ' + Tools.Axe + '</td>' +
				'<td>锯子: ' + Tools.Saw + '</td>' +
				'<td>镐子: ' + Tools.Pickaxe + '</td>' +
				'</tr>' +
				'</tbody>' +
				'</table>' +
				'</p>' +
				'</a>' +
				'<a href="#" class="list-group-item border">' +
				'<h4 class="list-group-item-heading">扩地: ' + expandFieldCount + '</h4>' +
				'<p class="list-group-item-text">' +
				'<table class="table" style="font-size: 13px;margin-bottom: -10px;">' +
				'<tbody>' +
				'<tr>' +
				'<td>桩标: ' + expandField['Marker Stake'] + '</td>' +
				'<td>地契: ' + expandField['Land Deed'] + '</td>' +
				'<td>木槌: ' + expandField.Mallet + '</td>' +
				'</tr>' +
				'</tbody>' +
				'</table>' +
				'</p>' +
				'</a>' +
				'<a href="#" class="list-group-item border">' +
				'<h4 class="list-group-item-heading">统计</h4>' +
				'<p class="list-group-item-text">' +
				'<table class="table" style="font-size: 13px;margin-bottom: -10px;">' +
				'<tbody>' +
				'<tr>' +
				'<td>粮仓: ' + data._BASICINFO_.silos_now + '/' + data._BASICINFO_.silos_max + '</td>' +
				'<td>货仓: ' + data._BASICINFO_.barn_now + '/' + data._BASICINFO_.barn_max + '</td>' +
				'</tr>' +
				'<tr>' +
				'<td>货物: ' + cargoToal + '</td>' +
				'<td>小麦: ' + wheatCount + '</td>' +
				'</tr>' +
				'</tbody>' +
				'</table>' +
				'</p>' +
				'</a>' +
				'</div>' +
				'</div>' +
				'</div>' +
				'</div>' +
				'</div>' +
				'</div>';

			$('div[name=according_start]').append(template);
		}


	});



	$('#add-account-form').formValidation({
		framework: 'bootstrap',
		icon: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			farm_name: {
				validators: {
					notEmpty: {
						message: '农场名必须填写'
					}
				}
			}
		}
	}).on('success.form.fv', function(e) {
		e.preventDefault();

		$.ajax({
			type: 'post',
			dataType: 'html',
			url: base + 'api/create',
			data: $('#add-account-form').serialize(),
			success: function (d) {
				$('#add-account-modal').modal('toggle');
				d = $.parseJSON(d);
				if (d.success) {
					alert('添加成功,服务器大概需要30s进行预登录处理,请稍后刷新.');
				} else {
					alert(d.msg);
				}
			},
			error: function(d) {
				$('#add-account-modal').modal('toggle');
				alert('出现了严重错误!!!!!')
			}
		});

	});

});